/*
 * NodeInput.h
 *
 *  Created on: 30/10/2011
 *      Author: godinho
 */

#ifndef NODEINPUT_H_
#define NODEINPUT_H_

#include <vector>

class CarBundle;

class NodeInput {
public:
	NodeInput(int timeToArrive);

	virtual ~NodeInput();

	int getQueueSize();

	/**
	 * Decrement amount from cars on toBeTransfered state.
	 *
	 * At the end of step, if still there are cars on this state, they will be
	 * moved to the node queue.
	 */
	int setAmountTransfered(int amount);

	void decrementTimeStepToArrive();

	void addCarsOnTrip(int qnt);

	int getTimeToArrive();

	int getFlush();

	int popAll();

	int getCarsToTransfer();

	/**
	 * Change all cars that are on toTransferState to onQueue state.
	 */
	void transferToQueue();

private:
	int mCarsOnQueue;
	int mLastMeasuredFlush;
	std::vector<CarBundle*> mCarsOnTrip;
	int mTimeToArrive;

	/*
	 * This field doesn't have any mean to simulation analysis. It is only a
	 * design decision to avoid that cars are added to a queue without need.
	 *
	 * <h1>A little more about the reason to this field</h1>
	 * <p>
	 * 	 The initial design previewed that all cars that leave the "on trip"
	 * 	 condition should be added to mCarsOnQueue. The values on this variable
	 * 	 would be used to register the size of queue on a determined
	 * 	 intersection. But this design decision brings a strange behavior, the
	 * 	 cars are always counted on queues, even though it has not halted in
	 * 	 front of a signal or in front of a hard traffic. The result was that in
	 * 	 simulation we could see cars on queues even when there is no reason
	 * 	 to it be showed as halted.
	 *
	 * 	 To solve this problem, cars are not put directly to a queue anymore.
	 * 	 During the time of a step execution, a car is moved to an intermediate
	 * 	 state, that we called as "Cars to be transfered". This state are used
	 * 	 only between the start and the end of a step, this means that this
	 * 	 value is not available on simulation listeners, so all listener will
	 * 	 display this value as 0.
	 *
	 * 	 During the step execution, the value for this attribute is calculated
	 * 	 and before the step end this value are distributed to the targets
	 * 	 node or to the queue. Basically the following are the steps:
	 *
	 *   <ol>
	 *   	<li>Calculate the numbe of cars on toBeTransfered state</li>
	 *   	<li>Transfer cars</li>
	 *   	<li>Set toBeTransfered to 0</li>
	 *   </ol>
	 *
	 *   Important assumptions: because of this strategy, all cars will take 1
	 *   timestep to cross an intersection.
	 *
	 *
	 * </p>
	 */
	int mCarsToTransfer;
};

#endif /* NODEINPUT_H_ */
